package com.fengkai.filepost.pdfoutstream.test.common.mq.producer;

import io.netty.channel.DefaultChannelId;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Fengkai
 * @creat 2021/12/30
 */
public class DelayProducer {
    //延迟消息生产者
       public static void main(String[] args) throws  Exception{
           DefaultMQProducer mqProducer = new DefaultMQProducer("pg");
           mqProducer.setNamesrvAddr("192.168.157.129:9876");
           DefaultChannelId.newInstance();
           mqProducer.start();

           for (int i = 0; i < 1000; i++) {
               byte[] body = ("Hi," + "TopicDelay" +  i).getBytes();
               Message message = new Message("TopicDelay", "topicDelay", body);
               //设置延迟等级
               message.setDelayTimeLevel(2);
               SendResult sendResult = mqProducer.send(message);
               //输出消息被发送的时间
               System.out.println(new SimpleDateFormat("mm:ss").format(new Date()));
               System.out.println(" ," + sendResult);
           }
       }
}
